package com.vpx.server.controller.system;

import com.vpx.server.bean.order.authority.LoginOrder;
import com.vpx.server.bean.result.system.AuthMenuResult;
import com.vpx.server.bean.result.system.UserDetailResult;
import com.vpx.server.framework.authority.*;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.service.system.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lihui
 * @title: vpx
 * @module system
 * @description: 系统登录
 * @date 2021/9/11 17:44
 */
@RestController
@RequestMapping(value = "/api")
@Api(tags = "登录、登出、授权菜单")
public class LoginController {
    @Autowired
    AuthorityService authorityService;

    @PostMapping("login")
    @ApiOperation("登录")
    @RequestAuth(poicy = AuthPoicy.UNLOGIN)
    public ResultMsg login(@RequestBody LoginOrder order) {
        return authorityService.login(order);
    }

    @GetMapping("logout")
    @ApiOperation(value = "退出")
    @RequestAuth(poicy = AuthPoicy.LOGIN)
    public ResultMsg logout() {

        SessionInfo sessionInfo = SessionUtil.getSession();
        sessionInfo.setClear(0);

        return ResultMsg.success(null,"退出系统成功");
    }

    @GetMapping("queryUserInfo")
    @ApiOperation(value = "获取登录用户信息")
    @RequestAuth(poicy = AuthPoicy.LOGIN)
    public ResultMsg<UserDetailResult> queryUserInfo() {
    	Long userId = SessionUtil.getSession().getSessionUser().getId();
        return authorityService.queryUserInfo(userId);
    }
    
    @GetMapping("getAuthMenu")
    @ApiOperation(value = "左侧授权菜单")
    @RequestAuth(poicy = AuthPoicy.LOGIN)
    public ResultMsg<AuthMenuResult> getAuthMenu() {
        Long userId = SessionUtil.getSession().getSessionUser().getId();
        return authorityService.getAuthMenu(userId);
    }

    @RequestAuth(poicy = AuthPoicy.UNLOGIN)
    public ResultMsg test(@RequestBody LoginOrder order) {

        return ResultMsg.success(null,"操作成功");
    }

    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg add(@RequestBody LoginOrder order) {
        return ResultMsg.success(null,"操作成功");
    }

    /**
     * allows={"/api/role/edit","/api/role/delete"} 表示允许拥有角色编辑、删除权限的用户访问本接口
     * @param order
     * @return
     */
    @RequestAuth(poicy = AuthPoicy.AUTH, allows={"/api/role/edit","/api/role/delete"})
    public ResultMsg add2(@RequestBody LoginOrder order) {
        return ResultMsg.success(null,"操作成功");
    }

}
